<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['name', validatorRules.name]"
                placeholder="请输入员工姓名"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['idCard', validatorRules.idCard]"
                placeholder="请输入身份证"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['email']"
                placeholder="请输入邮箱"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="电话号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['phone', validatorRules.phone]"
                placeholder="请输入电话号码"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-form-item v-show="false">
            <a-input :disabled="disableSubmit" v-decorator="['name']"></a-input>
          </a-form-item>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JUpload from '@/components/jeecg/JUpload'
import JImageUpload from '@/components/jeecg/JImageUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JCategorySelect from '@/components/jeecg/JCategorySelect'

export default {
  name: 'HrEmployeeModal',
  components: {
    JDate,
    JUpload,
    JImageUpload,
    JDictSelectTag,
    JSearchSelectTag,
    JCategorySelect,
  },
  data() {
    return {
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: {
          rules: [{ required: true, message: '请输入员工姓名!' }],
        },
        idCard: {
          rules: [{ required: true, message: '请输入身份证!' }],
        },
        /* email: {
          rules: [
            { required: true, message: '请输入邮箱!' },
            { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!' },
          ],
        }, */
        phone: {
          rules: [
            { required: true, message: '请输入电话号码!' },
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的电话号码!' },
          ],
        },
      },
      url: {
        get: '/online/api/getHrEmployee',
      },
      urlEdit:'/online/api/createEsignHrEmployee',
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      if(record.update=='Y'){
        this.urlEdit = '/online/api/updateEsignHrEmployee';
      }else{
        this.urlEdit = '/online/api/createEsignHrEmployee';
      }
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.partyId) {
        httpAction(this.url.get, { partyId: record.partyId }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'name',
                  'idCard',
                  'email',
                  'phone',
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = this.urlEdit
          let method = 'put'
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'name',
          'idCard',
          'email',
          'phone',
        )
      )
    },
  },
}
</script>
